<template>
  <el-dialog v-model="shade.show" center title="登录" width="400px" destroy-on-close>
    <div class="s-loginshade">
      <el-form label-width="80px">
        <el-form-item label="账号:">
          <el-input v-model="username"></el-input>
        </el-form-item>
        <el-form-item label="密码:">
          <el-input v-model="password"></el-input>
        </el-form-item>
        <el-form-item class="login-sub-content">
          <el-button type="primary" @click="loginSubmin">登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<script setup>
import { ref, toRef, inject } from "vue";
const http = inject("ajax");
import { ElMessage } from "element-plus";

let props = defineProps({
  shadeObj: {
    type: Object,
    default() {
      return {
        show: false,
      };
    },
  },
});
const shade = toRef(props, "shadeObj"); //变成响应式的数据
const username = ref("");
const password = ref("123456");

const loginSubmin = () => {
  if (username.value == "") {
    ElMessage("请输入账号");
    return;
  }
  if (password.value == "") {
    ElMessage("请输入密码");
    return;
  }
  let prams = {
    userName: username.value,
    passWord: password.value,
  };
  http("/iaiqsmde9fea569965ed56ab98ea0501752fe631902841e/login", prams, "post")
    .then(res => {
      let { success, token, code } = res;
      if (success && code == 0 && token && token != "") {
        su.cook.setcookie("token", 778899);
        su.cook.setcookie("username", username.value);
        su.cook.setcookie("userid", Date.parse(new Date()));
        su.cook.setcookie("iswebsoket", 1);
        ElMessage({
          message: "登录成功",
          type: false,
        });
        shade.show = false;
        setTimeout(() => {
          location.href = "./";
        }, 800);
      }
    })
    .catch(err => {
      console.log(err.message || "数据异常");
    });
};
</script>
